package com.sdhs.system.syncdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdhs.system.syncdata.domain.SyncSysUserDept;
import com.sdhs.system.syncdata.mapper.SyncSysUserDeptMapper;
import com.sdhs.system.syncdata.service.SyncSysUserDeptService;
import com.sdhs.system.syncdata.util.TextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户隶属机构(SysUserDept)表服务实现类
 *
 * @author makejava
 * @since 2023-01-31 12:41:19
 */
@Service("sysUserDeptService")
public class SyncSysUserDeptServiceImpl extends ServiceImpl<SyncSysUserDeptMapper, SyncSysUserDept> implements SyncSysUserDeptService {
    @Resource
    private SyncSysUserDeptMapper sysUserDeptDao;
    @Autowired
    private SyncSysUserDeptService sysUserDeptService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SyncSysUserDept queryById(Long id) {
        return this.sysUserDeptDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param sysUserDept 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<SyncSysUserDept> queryByPage(SyncSysUserDept sysUserDept, PageRequest pageRequest) {
        long total = this.sysUserDeptDao.count(sysUserDept);
        return new PageImpl<>(this.sysUserDeptDao.queryAllByLimit(sysUserDept, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sysUserDept 实例对象
     * @return 实例对象
     */
    @Override
    public SyncSysUserDept insert(SyncSysUserDept sysUserDept) {
        this.sysUserDeptDao.insert(sysUserDept);
        return sysUserDept;
    }

    /**
     * 修改数据
     *
     * @param sysUserDept 实例对象
     * @return 实例对象
     */
    @Override
    public SyncSysUserDept update(SyncSysUserDept sysUserDept) {
        this.sysUserDeptDao.update(sysUserDept);
        return this.queryById(sysUserDept.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.sysUserDeptDao.deleteById(id) > 0;
    }

    @Override
    public boolean updateDeptByUser(String userId, List<String> deptIds,String creator) {
        QueryWrapper<SyncSysUserDept> sysUserDeptQueryWrapper=new QueryWrapper<>();
        sysUserDeptQueryWrapper.eq("user_id",userId);
        sysUserDeptQueryWrapper.notLike("dept_id","YG%");
        int delete = sysUserDeptDao.delete(sysUserDeptQueryWrapper);
        String deptId="";
        List<SyncSysUserDept> list=new ArrayList<>();
        int result=0;
        int size=0;
        boolean b=false;
        if (deptIds!=null && !deptIds.isEmpty()) {
            size=deptIds.size();
            for(int i=0;i<size;i++){
                deptId=deptIds.get(i);
                SyncSysUserDept sysUserDept = new SyncSysUserDept();
                sysUserDept.setUserId(userId);
                sysUserDept.setDeptId(deptId);
                sysUserDept.setCompanyId(deptId);
                sysUserDept.setCreator(creator);
                sysUserDept.setCreateDate(new Date());
                sysUserDept.setUpdater(creator);
                sysUserDept.setUpdateDate(new Date());
                if(i==0){
                    sysUserDept.setHasMain(1);
                }else{
                    sysUserDept.setHasMain(0);
                }
                list.add(sysUserDept);
//                    int insert1 = sysUserDeptMapper.insert(sysUserDept);
            }

            b = this.sysUserDeptService.saveBatch(list, size);

        }
        return b;
    }
    @Override
    public boolean updateManageDeptByUser(String userId, String deptId,String creator) {


        boolean b=false;
        QueryWrapper<SyncSysUserDept> sysUserDeptQueryWrapper=new QueryWrapper<>();
        sysUserDeptQueryWrapper.eq("user_id",userId);
        sysUserDeptQueryWrapper.like("dept_id","YG%");
        int delete = sysUserDeptDao.delete(sysUserDeptQueryWrapper);
        if (deptId!=null&& TextUtil.isNotNull(deptId)) {

            SyncSysUserDept sysUserDept = new SyncSysUserDept();
                sysUserDept.setUserId(userId);
                sysUserDept.setDeptId(deptId);
                sysUserDept.setCompanyId(deptId);
                sysUserDept.setCreator(creator);
                sysUserDept.setCreateDate(new Date());
                sysUserDept.setUpdater(creator);
                sysUserDept.setUpdateDate(new Date());

                    sysUserDept.setHasMain(0);

            SyncSysUserDept insert = this.sysUserDeptService.insert(sysUserDept);
            b=true;

        }
        return b;
    }
}
